package day13.demo1.maipiao.demo3;

/**
 * @Auther:小陈的Java
 * 同步方法解决卖票问题
 */
public class WindowTest3 {
    public static void main(String[] args) {
        Thread thread1 = new Window3("窗口1");
        Thread thread2 = new Window3("窗口2");
        Thread thread3 = new Window3("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
class Window3 extends Thread{
    private static int ticket=100;
    private static boolean tag = true;
    public Window3(String name){
        super(name);
    }
    @Override
    public void run() {
        while (tag){
            sell();
        }
    }
    public static synchronized void sell(){
        if(ticket>0){
            try {
                System.out.println(Thread.currentThread().getName()+":卖票，卖第"+ticket+"票");
                Thread.sleep(10);
                ticket--;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else {
            tag = false;
        }
    }

}